package com.yytek.upgrade.controller;

import java.util.List;

import com.yytek.common.core.domain.R;
import com.yytek.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yytek.common.log.annotation.Log;
import com.yytek.common.log.enums.BusinessType;
import com.yytek.common.security.annotation.RequiresPermissions;
import com.yytek.upgrade.domain.UpgradeHistory;
import com.yytek.upgrade.service.IUpgradeHistoryService;
import com.yytek.common.core.web.controller.BaseController;
import com.yytek.common.core.web.domain.AjaxResult;
import com.yytek.common.core.utils.poi.ExcelUtil;
import com.yytek.common.core.web.page.TableDataInfo;

/**
 * 升级记录Controller
 * 
 * @author yytek-iot
 * @date 2022-09-06
 */
@Api(value = "升级记录控制器", tags = {"升级记录管理"})
@RestController
@RequestMapping("/history")
public class UpgradeHistoryController extends BaseController
{
    @Autowired
    private IUpgradeHistoryService upgradeHistoryService;

    /**
     * 根据设备id和执行状态查询升级记录
     */
    @ApiOperation("根据设备id和执行状态查询升级记录")
    @PostMapping(value = "/selectUpgradeHisByIotId")
    public R<List<UpgradeHistory>> selectUpgradeHisByIotId(String iotId,int status)
    {
       return R.ok(upgradeHistoryService.selectUpgradeHisByIotId(iotId,status));
    }

    /**
     * 分页查询升级记录列表
     */
    @ApiOperation("分页查询升级记录列表")
    @RequiresPermissions("upgrade:history:list")
    @GetMapping("/list")
    public TableDataInfo list(UpgradeHistory upgradeHistory)
    {
        startPage();
        //查询创建人
        String userId = SecurityUtils.getUserId().toString();
        upgradeHistory.setUserId(userId);
        List<UpgradeHistory> list = upgradeHistoryService.selectUpgradeHistoryList(upgradeHistory);
        return getDataTable(list);
    }

    /**
     * 导出升级记录列表
     */
    @ApiOperation("导出升级记录列表")
    @RequiresPermissions("upgrade:history:export")
    @Log(title = "升级记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UpgradeHistory upgradeHistory)
    {
        List<UpgradeHistory> list = upgradeHistoryService.selectUpgradeHistoryList(upgradeHistory);
        ExcelUtil<UpgradeHistory> util = new ExcelUtil<UpgradeHistory>(UpgradeHistory.class);
        util.exportExcel(response, list, "升级记录数据");
    }

    /**
     * 获取升级记录详细信息
     */
    @ApiOperation("获取升级记录详细信息")
    @RequiresPermissions("upgrade:history:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(upgradeHistoryService.selectUpgradeHistoryById(id));
    }

    /**
     * 更新设备升级记录状态
     */
    @ApiOperation("更新设备升级记录状态")
    @Log(title = "更新设备升级记录状态", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/changeStrategyHisStatus")
    public R changeStrategyHisStatus(String iotId, Long upgradeStrategyId, int status)
    {
        int rows = upgradeHistoryService.changeStrategyHisStatus(iotId,upgradeStrategyId,status);
        if(rows > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }


    /**
     * 新增升级记录API
     */
    @ApiOperation("新增升级记录API")
    @Log(title = "升级记录", businessType = BusinessType.INSERT)
    @PutMapping(value = "/addUpgradeHistory")
    public R addUpgradeHistory(@RequestBody UpgradeHistory upgradeHistory)
    {
        int rows = upgradeHistoryService.insertUpgradeHistory(upgradeHistory);
        if(rows > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }


    /**
     * 新增升级记录
     */
    @ApiOperation("新增升级记录")
    @RequiresPermissions("upgrade:history:add")
    @Log(title = "升级记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UpgradeHistory upgradeHistory)
    {
        return toAjax(upgradeHistoryService.insertUpgradeHistory(upgradeHistory));
    }

    /**
     * 修改升级记录
     */
    @ApiOperation("修改升级记录")
    @RequiresPermissions("upgrade:history:edit")
    @Log(title = "升级记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UpgradeHistory upgradeHistory)
    {
        return toAjax(upgradeHistoryService.updateUpgradeHistory(upgradeHistory));
    }

    /**
     * 删除升级记录
     */
    @ApiOperation("删除升级记录")
    @RequiresPermissions("upgrade:history:remove")
    @Log(title = "升级记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(upgradeHistoryService.deleteUpgradeHistoryByIds(ids));
    }
}
